<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\AdminUsers;
use App\Models\SVoucherInfo;
use App\User;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class VoucherController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('抵扣卷')
			->description('列表')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content
			->header('抵扣卷')
			->description('详情')
			->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content
			->header('抵扣卷')
			->description('编辑')
			->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content
			->header('抵扣卷')
			->description('新增')
			->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid(new SVoucherInfo);

		$grid->id('Id')->sortable();
		$grid->title('标题');
		$grid->voucher('抵扣值')->display(function ($p) {
			return sprintf("%.2f", $p / 100);
		});
		$grid->desc('备注');
		$grid->users()->name('学生');
		$grid->use_time('使用时间');
		$grid->adminUsers()->name('操作人');
		$grid->created_at('创建时间');
		$grid->updated_at('更新时间');

		//禁用导出
		$grid->disableExport();

		$grid->actions(function ($actions) {
			if ($actions->row->use_time > 0) {
				//使用过的不可编辑/删除
				$actions->disableDelete();
				$actions->disableEdit();
			}
		});

		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show(SVoucherInfo::findOrFail($id));

		$show->id('Id');
		$show->title('标题');
		$show->voucher('抵扣值')->as(function ($p) {
			return sprintf("%.2f", $p / 100);
		});
		$show->desc('备注');
		$show->user_id('学生')->as(function ($p) {

			return User::find($p)->name;
		});
		$show->use_time('使用时间');
		$show->admin_id('操作人')->as(function ($p) {

			return AdminUsers::find($p)->name;
		});
		$show->created_at('创建时间');
		$show->updated_at('更新时间');

		$show->panel()
			->tools(function ($tools) {
				$tools->disableEdit();
				// $tools->disableList();
				$tools->disableDelete();
			});
		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form(new SVoucherInfo);

		$form->text('title', '标题')->rules('required');
		$form->number('voucher', '抵扣值(单位分)')->min(0);
		$form->text('desc', '备注');
		// $form->number('user_id', '学生')->rules('required');
		$users = User::pluck('name', 'id');
		// dd($users);
		$form->select("user_id", "学生")->options($users)->rules('required');

		$form->hidden('admin_id')->value(\Admin::user()->id);
		// $form->datetime('use_time', 'Use time')->default(date('Y-m-d H:i:s'));

		$form->footer(function ($footer) {

			// 去掉`查看`checkbox
			$footer->disableViewCheck();

			// 去掉`继续编辑`checkbox
			$footer->disableEditingCheck();

			// 去掉`继续创建`checkbox
			$footer->disableCreatingCheck();

		});

		return $form;
	}
}
